package gui.userslist;

import java.util.ArrayList;

import javax.swing.AbstractListModel;

import user.User;
import user.UsersListObserver;

public class UsersListModel extends AbstractListModel implements UsersListObserver {

	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private ArrayList <User> users ;
	
	public UsersListModel () {
		this.users = new ArrayList <User> () ;
	}
	
	@Override
	public Object getElementAt(int index) {
		return this.users.get(index);
	}

	@Override
	public int getSize() {
		return this.users.size();
	}

	@Override
	public void fireNewUser(User u) {
		this.users.add(u) ;
		fireIntervalAdded(this, this.users.size() - 1, this.users.size() - 1) ;
	}

	@Override
	public void fireUpdateUser(User oldUser, User newUser) {
		int index = this.users.indexOf(newUser) ;
		this.users.set(index, newUser) ;
		fireContentsChanged(this, index, index) ;
		
	}

	@Override
	public void fireDeleteUser(User u) {
		int index = this.users.indexOf(u) ;
		this.users.remove(index) ;
		fireIntervalRemoved(this, index, index) ;
	}

}
